package com.soft.line.controller;

import com.github.pagehelper.PageInfo;
import com.soft.line.entity.ButtonInfo;
import com.soft.line.entity.MenuButtonInfo;
import com.soft.line.entity.MenuInfo;
import com.soft.line.service.ButtonInfoService;
import com.soft.line.service.CodeDetailService;
import com.soft.line.service.MenuInfoService;
import com.soft.line.util.exception.CommonResult;
import com.soft.line.util.exception.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Api(description = "菜单接口")
@Slf4j
@RestController
@RequestMapping("/systemAdmin")
public class MenuInfoController {

    @Autowired
    private MenuInfoService menuInfoService;
    @Autowired
    private ButtonInfoService buttonInfoService;

    @PostMapping("/menus")
    @ApiOperation(value = "查询所有菜单列表")
    public CommonResult getAllMenus(@RequestBody Map<String,Object> map) {
        return CommonResult.success(menuInfoService.selectAll(map));
    }

    @GetMapping("/selectALlMenuName")
    @ApiOperation(value = "查询所有菜单名称")
    public CommonResult<List<String>> getAllMenuNames() {
        return CommonResult.success(menuInfoService.selectALlMenuName());
    }

    @ApiOperation(value = "获取菜单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId", value = "菜单ID", required = true, dataType = "Integer", paramType = "path")
    })
    @GetMapping("/getMenu")
    public CommonResult<MenuInfo> getMenuInfoByID(@RequestBody Map<String,Object> map) {
        Integer id = (Integer) map.get("id");
        return CommonResult.success(menuInfoService.selectByPrimaryKey(id));
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/menu")
    public CommonResult deleteMenu(@RequestBody Map<String,Object> map){
        menuInfoService.delete((Integer) map.get("id"));
        return CommonResult.success("删除成功");
    }

    @ApiOperation(value = "保存菜单")
    @PostMapping("/menu")
    public CommonResult saveMenu(@RequestBody Map<String,Object> map) {
        MenuInfo menuInfo = new MenuInfo();
        menuInfo.setId((Integer) map.get("id"));
        if (map.get("menuName")==null||"".equals((String)map.get("menuName"))){
            return CommonResult.failed("菜单名称不能为空");
        }else{
            //新增时判断menuName是否重复
            menuInfo.setMenuName(map.get("menuName").toString());
            if (menuInfoService.countMenuName(menuInfo.getId(),menuInfo.getMenuName())>0){
                return CommonResult.failed("菜单名称已存在");
            }
        }

        menuInfo.setMenuIcon(map.get("menuIcon")==null?"":map.get("menuIcon").toString());
        menuInfo.setMenuUrl(map.get("menuUrl")==null?"":map.get("menuUrl").toString());
        if (map.get("orderBy")==null){
            return CommonResult.failed("菜单排序不能为空");
        }else{
            menuInfo.setOrderBy((map.get("orderBy")==null?"":map.get("orderBy").toString()));
            if (menuInfoService.countOrderBy(menuInfo.getId(),menuInfo.getOrderBy())>0){
                return CommonResult.failed("菜单排序已存在");
            }
        }

        //判断父级菜单是否存在
        menuInfo.setpId((Integer) map.get("pId"));
        if (menuInfo.getpId()==-1){
            menuInfo.setState(1);
        }else {
            MenuInfo pMenu = menuInfoService.selectParentMenu(menuInfo.getpId());
            if (pMenu==null){
                return CommonResult.failed("父级菜单不存在");
            }
            menuInfo.setState(pMenu.getState()+1);
        }

        menuInfo.setModifyTime(new Date());
        menuInfo.setModifyPerson("admin");
        List<ButtonInfo> buttonInfos =new ArrayList<ButtonInfo>();
        for (String buttonId : (List<String>)map.get("buttonList")){
            ButtonInfo buttonInfo = buttonInfoService.selectByButtonId(buttonId);
            buttonInfos.add(buttonInfo);
        }
        menuInfo.setButtonInfoList(buttonInfos);
        if(menuInfo.getId()!=null){
            menuInfoService.updateMenInfo(menuInfo);
        }else{
            menuInfoService.addMenuInfo(menuInfo);
        }
        return CommonResult.success("保存菜单成功");

    }

}
